// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/index';
import Viser from 'viser-vue'
Vue.use(Viser)
import { Button, Form, Checkbox, Icon, Input, Card, Breadcrumb, Row, Col, InputNumber, Layout, Menu, Radio, Select, Table, TimePicker, LocaleProvider, message, Tabs, DatePicker, Modal, Avatar, Dropdown, Upload, Popconfirm, Badge, Switch, Transfer, Cascader, TreeSelect, Steps, Divider } from "ant-design-vue";

Vue.use(Button);
Vue.use(Form);
Vue.use(Checkbox);
Vue.use(Icon);
Vue.use(Input);
Vue.use(Card);
Vue.use(Breadcrumb);
Vue.use(Row);
Vue.use(Col);
Vue.use(InputNumber);
Vue.use(Layout);
Vue.use(Menu);
Vue.use(Radio);
Vue.use(Select);
Vue.use(Table);
Vue.use(TimePicker);
Vue.use(LocaleProvider);
Vue.use(Tabs);
Vue.use(DatePicker);
Vue.use(Modal);
Vue.use(Avatar);
Vue.use(Dropdown);
Vue.use(Upload);
Vue.use(Badge);
Vue.use(Switch);
Vue.use(Transfer);
Vue.use(Cascader);
Vue.use(TreeSelect);
Vue.use(Steps);
Vue.use(Divider);


Vue.prototype.$message = message;
Vue.prototype.$confirm = Modal.confirm;
Vue.prototype.$error = Modal.error;
Vue.prototype.$message = message;

import axios from 'axios'  //引入封装的axios
import './axios/axios'    //axios.js的路径
import md5 from 'js-md5';
import Cookies from 'js-cookie'
Vue.prototype.axios = axios;  //全局注册,任何组件都能直接使用
Vue.prototype.$md5 = md5;
Vue.prototype.$Cookies = Cookies;


Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
  if (to.matched.some(res => res.meta.requireLogin)) {
    // 判断是否需要登录权限
    if (window.localStorage.getItem("loginToken")) {
      // 判断是否登录
      if (store.state.loginToken) {
        next();
      } else {
        Modal.error({
          title: '温馨提示',
          content: '登录状态信息过期,请重新登录'
        });
        next({
          path: "/login",
          query: { redirect: to.fullPath }
        });
      }
    } else {
      // 没登录则跳转到登录界面
      next({
        path: "/login",
      });
    }
  } else {
    next();
  }
});

// 请求文件内容
function getServerConfig() {
  return new Promise((resolve, reject) => {
    axios.get('./static/serverConfig.json').then(result => {
      // console.log(result)
      let config = result.data;
      // console.log(config)
      for (let key in config) {
        Vue.prototype[key] = config[key];
      }
      resolve();
    })
  })
}

// 请求文件内容及创建实例
async function main() {
  await getServerConfig();
  new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>',
    store,//加入sotre的配置
  })
}
// 方法初始执行
main();
